unit Highscore;

interface

uses
  System.IO, HighscoreEntry, System.Collections, HighscoreEntryComparer;

type
  THighscore = class
  private
    { Private Declarations }
    filePath : String;
  public
    constructor Create;

    procedure AddScore(name : String; score : System.Decimal);
    function GetAllScores() : ArrayList;
  end;

implementation

constructor THighscore.Create;
begin
  inherited Create;

  self.filePath := System.IO.FileInfo.Create('highscore.ts').FullName;
end;

procedure THighscore.AddScore(name: string; score: Decimal);
var
  fs : FileStream;
  bw : BinaryWriter;
  e : THighscoreEntry;

begin
  fs := FileStream.Create(self.filePath, FileMode.Append);
  bw := BinaryWriter.Create(fs);

  e := THighscoreEntry.Create(name, score);
  e.Write(bw);
  bw.Flush();
  fs.Close();
end;

function THighscore.GetAllScores() : ArrayList;
var
  fs : FileStream;
  br : BinaryReadeR;
  e : THighscoreEntry;
  arr : ArrayList;
  
begin
  arr := ArrayList.Create();

  if (System.IO.File.Exists(self.filePath)) then
  begin
    fs := FileStream.Create(self.filePath, FileMode.OpenOrCreate);
    br := BinaryReader.Create(fs);

    while fs.Position < fs.Length do
    begin
      arr.Add(THighscoreEntry.Read(br));
    end;

    fs.Close();
  end;

  arr.Sort(THighscoreEntryComparer.Create());
  result := arr;
end;




end.
